package com.example.unisys.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.unisys.common.Response;
import com.example.unisys.common.STATUS;
import com.example.unisys.entity.MainCourse;
import com.example.unisys.service.CourseSaleService;
import com.example.unisys.service.MainCourseService;
import com.example.unisys.service.SubCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/apis/v1/sales", produces = "application/json; charset=utf-8")
public class CourseSaleController {

    @Autowired
    private CourseSaleService coursesaleService;

    @Autowired
    private MainCourseService mainCourseService;

    @Autowired
    private SubCourseService subCourseService;

    /*
     * 更新课程销量
     * */
    @RequestMapping(value = "/update", method = {RequestMethod.PUT})
    public Response updateCourseValid(@RequestBody JSONObject jsonParam) {
        MainCourse mainCourse = JSONObject.parseObject(jsonParam.toJSONString(), MainCourse.class);
        int ret_code = mainCourseService.update(mainCourse);
        if (ret_code == 1)
            return Response.builder().status(STATUS.SUCCESS).build();
        return Response.builder().status(STATUS.BAD_REQUEST).build();
    }

    /*
    * 根据学校查询对应销量
    * */
 /*   @RequestMapping(value = "", method = {RequestMethod.GET})
    public Response getSales(@RequestParam("main_school") String main_school, @RequestParam("sub_school") String sub_school,
                             @RequestParam("limit") int limit, @RequestParam("page") int page, @RequestParam("searchText") String searchText, @RequestParam("searchType") int searchType) {
        int newlimit = (page - 1) * limit;
        int pagesize = limit;
        Map<String, Object> hs1 = new HashMap<>();
        Map<String, Object> hs2 = new HashMap<>();
        hs1.put("start", newlimit);
        hs1.put("pagesize", pagesize);
        hs1.put("main_school", main_school);
        hs2.put("main_school", main_school);
        List<CourseSale> ans = new ArrayList<>();
        List<CourseSale> ls;
        if (main_school.equals(sub_school)) {
            //主校
            if (searchText.equals(""))
                *//*加入search逻辑*//*
                ls = coursesaleService.getSales(hs1);
            else {
                if (searchType == 1) {
                    if (Tools.isNumeric(searchText)) {
                        hs1.put("course_id", searchText);
                        hs2.put("course_id", searchText);
                    }
                } else if (searchType == 2) {
                    hs1.put("course_name", searchText);
                    hs2.put("course_name", searchText);
                }
                ls = coursesaleService.getSalesBySearch(hs1);
            }
            List<MainCourse> ms;
            ms = mainCourseService.getBySchool(main_school);
            for (CourseSale c : ls) {
                for (MainCourse m : ms) {
                    if (c.getCourse_id().equals(m.getCourse_id())) {
                        c.setNorm_duration(m.getNorm_duration());
                        c.setNorm_sum(m.getNorm_sum());
                        c.setNorm_price(m.getNorm_price());
                        c.setCross_price(m.getCross_price());
                        c.setIs_valid(m.getIs_valid());
                        ans.add(c);
                    }
                }
            }
        } else {
            hs1.put("sub_school", sub_school);
            hs2.put("sub_school", sub_school);
            if (searchText.equals("") || (!Tools.isNumeric(searchText)))
                *//*加入search逻辑*//*
                ls = coursesaleService.getSales(hs1);
            else {
                if (searchType == 1)
                    hs1.put("course_id", Integer.parseInt(searchText));
                else
                    hs1.put("course_name", searchText);
                ls = coursesaleService.getSalesBySearch(hs1);
            }

            List<SubCourse> ss = subCourseService.getBySchool(main_school, sub_school, 0, 99999);
            for (CourseSale c : ls) {
                for (SubCourse s : ss) {
                    if (c.getCourse_id().equals(s.getCourse_id())) {
                        c.setNorm_duration(s.getNorm_duration());
                        c.setNorm_sum(s.getNorm_sum());
                        c.setNorm_price(s.getNorm_price());
                        c.setCross_price(s.getCross_price());
                        ans.add(c);

                    }
                }
            }
        }
        res.put("code", 20000);
        res.put("data", ans);
        res.put("total", coursesaleService.getSaleSum(hs2));
        return res;
    }
*/
}
